package com.yuanzi.base.auth;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yuanzi.base.constants.AuthConstants;
import com.yuanzi.base.redis.OuterHashRedisService;
import com.yuanzi.base.util.JSONUtils;

/**
 * 保存学校对产品服务的关系
 * @author choijung
 *
 */
@Service
public class AuthSchoolServicesRedisService {
	
	private static String serviceName = AuthConstants.AUTH_SERVICE_NAME;
	
	private static String redisName = AuthConstants.REDIS_SCHOOL_SERVICES;
	
	/*--------------存储学校对产品的有效期权限：Hash形式：zuul-service:school-services:schoolId(key):teachServiceId(field)=>购买的具体信息(value)---------------*/	
	
	/**
	 * 获取学校对某个产品服务的具体持有信息
	 * @param schoolId
	 * @param teachServiceId
	 * @return
	 */
	public static String getSchoolService(Object schoolId, Object teachServiceId) {
		return OuterHashRedisService.hget(serviceName, redisName + schoolId.toString(), teachServiceId.toString());
	}
	
	/**
	 * 批量获取学校对某个产品服务的具体持有信息
	 * @param schoolId
	 * @param teachServiceIds
	 * @return
	 */
	public static List<String> getSchoolServices(Object schoolId, Object... teachServiceIds){
		String[] fields = new String[teachServiceIds.length];
		for(int i = 0 ; i < teachServiceIds.length ; i++) {
			fields[i] = teachServiceIds[i].toString();
		}
		return OuterHashRedisService.hmget(serviceName, redisName + schoolId.toString(), fields);
	}
	
	/**
	 * 批量设置学校对某个产品服务的具体持有信息
	 * @param schoolId
	 * @param services
	 */
	public static void setSchoolServicesInBatch(Object schoolId, Map<String, String> services) {
		OuterHashRedisService.hmset(serviceName, redisName + schoolId.toString(), services);
	}
	
	/**
	 * 设置学校对某个产品服务的具体持有信息
	 * @param schoolId
	 * @param teachServiceId
	 * @param obj
	 */
	public static void setSchoolService(Object schoolId, Object teachServiceId, Object obj) {
		if(obj instanceof String) {
			OuterHashRedisService.hset(serviceName, redisName + schoolId.toString(), teachServiceId.toString(), obj.toString());
		}else {
			OuterHashRedisService.hset(serviceName, redisName + schoolId.toString(), teachServiceId.toString(), JSONUtils.toJSONString(obj));
		}	
	}
	
	/**
	 * 设置学校对某个产品服务过期
	 * @param schoolId
	 * @param teachServiceId
	 */
	public static void setSchoolServiceExpire(Object schoolId, Object teachServiceId) {
		JSONObject expired = new JSONObject();
		expired.put("expired", 1);
		OuterHashRedisService.hset(serviceName, redisName + schoolId.toString(), teachServiceId.toString(), expired.toJSONString());
	}
	
	/**
	 * 设置学校对某个产品服务无权限
	 * @param schoolId
	 * @param teachServiceId
	 */
	public static void setSchoolServiceNoAuth(Object schoolId, Object teachServiceId) {
		JSONObject noAuth = new JSONObject();
		noAuth.put("noAuth", 1);
		OuterHashRedisService.hset(serviceName, redisName + schoolId.toString(), teachServiceId.toString(), noAuth.toJSONString());
	}
	
	/**
	 * 删除学校对某个产品服务的具体持有信息
	 * @param schoolId
	 * @param teachServiceIds
	 */
	public static void delSchoolService(Object schoolId, Object... teachServiceIds) {
		OuterHashRedisService.hdel(serviceName, redisName + schoolId.toString(), teachServiceIds.toString());
	}
	
}
